package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;//菜品Mapper
    @Autowired
    private DishFlavorMapper dishFlavorMapper;//口味Mapper
    @Autowired
    private SetmealDishMapper setmealDishMapper;//套餐和菜品的Mapper
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 新增菜品和对应口味
     * @param dishDTO
     */
    @Override
    @Transactional
    public void saveWithFlavor(DishDTO dishDTO) {
        log.info("saveWithFlavor");
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        //向菜品表插入一条数据
        dishMapper.insert(dish);

        Long dishId = dish.getId();

        List<DishFlavor> flavors = dishDTO.getFlavors();

        if (flavors!=null&&flavors.size()>0){
            for (DishFlavor dishFlavor : flavors) {
                dishFlavor.setDishId(dishId);
            }
            //向口味表插入N条数据
            dishFlavorMapper.insertBatch(flavors);
        }

    }


    /**
     * 分页查询菜品
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        log.info("pageQuery");
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishVO> page=dishMapper.pageQuery(dishPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 菜品批量删除功能
     * @param ids
     */
    @Override
    @Transactional//事务注解
    public void deleteBatch(List<Long> ids) {
        log.info("deleteBatch");
        //判断当前菜品是否存在起售中的菜品
        for (Long id : ids) {
            Dish dish=dishMapper.getById(id);
            if (dish.getStatus()== StatusConstant.ENABLE){
                //当前菜品起售中，不能删除
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //判断当前菜品是否关联其他套餐
        List<Long> setmealIdByDishIds = setmealDishMapper.getSetmealIdByDishIds(ids);
        if (setmealIdByDishIds!=null&&setmealIdByDishIds.size()>0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

//        for (Long id : ids) {
//            //删除菜品数据
//            dishMapper.deleteById(id);
//            //删除口味数据
//            dishFlavorMapper.deleteByDishId(id);
//        }

//        优化
        //删除菜品数据
        dishMapper.deleteByIds(ids);
        //删除口味数据
        dishFlavorMapper.deleteByDishIds(ids);
    }

    /**
     * 根据ID查询菜品和口味数据
     * @param id
     * @return
     */
    @Override
    public DishVO getByid(Long id) {
        log.info("getByid");
        Dish dish=dishMapper.getById(id);
        List<DishFlavor> dishFlavors=dishFlavorMapper.getByDishId(id);
        DishVO dishVO=new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavors);
        return dishVO;
    }

    /**
     * 修改菜品
     * @param dishDTO
     */
    @Override
    public void update(DishDTO dishDTO) {
        log.info("update");
        Dish dish=new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        //修改菜品信息
        dishMapper.update(dish);
        //修改口味信息采用先删除口味信息再重新添加的方式
        //删除
        dishFlavorMapper.deleteByDishId(dishDTO.getId());
        //重新添加
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors!=null&&flavors.size()>0){
            for (DishFlavor dishFlavor : flavors) {
                dishFlavor.setDishId(dishDTO.getId());
            }
            //向口味表插入N条数据
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    @Override
    public void updateDishStatus(Integer status, Long id) {
        log.info("updateDishStatus");
        //查询菜品是否存在
        Dish byId = dishMapper.getById(id);
        if (byId!=null){
            //修改菜品状态
            byId.setStatus(status);
            dishMapper.update(byId);

            //需要将正在售卖的套餐也一起停止
            //根据关联表查询哪些套餐包含了这个菜
            if (status==0){
                List<Long> dishId=new ArrayList<>();
                dishId.add(id);
                List<Long> setmealIds = setmealDishMapper.getSetmealIdByDishIds(dishId);
                //批量禁用套餐
                setmealMapper.updateStatusByDishs(setmealIds);
            }

        }else{
            throw new RuntimeException("菜品不存在！");
        }
    }

    @Override
    public List<DishVO> listByCategoryId(Integer categoryId) {
        return dishMapper.listByCategoryId(categoryId);
    }

    @Override
    public List<DishVO> listWithFlavor(Long categoryId) {
        Integer cID=categoryId.intValue();
        //根据分类ID查询菜品
        List<DishVO> dishVoList = dishMapper.listByCategoryId(cID);
        //菜品查询菜品口味
        for (DishVO dishVO : dishVoList) {
            List<DishFlavor> dishFlavors = dishFlavorMapper.getByDishId(dishVO.getId());
            dishVO.setFlavors(dishFlavors);
        }
        return dishVoList;
    }


}
